﻿using System;
using System.Diagnostics;
using System.IO;

namespace SfsTools.Core
{
    /// <summary>
    /// Represents the volume descriptor.
    /// </summary>
    internal class VolumeIdentifierEntry : IndexEntry
    {
        #region Properties

        /// <summary>
        /// Gets or sets the time stamp.
        /// </summary>
        public TimeStamp TimeStamp
        {
            [DebuggerStepThrough]
            get
            {
                using (BinaryReader br = new BinaryReader(new MemoryStream(FBuffer, false)))
                {
                    br.BaseStream.Position = 4;
                    return new TimeStamp(br.ReadInt64());
                }
            }
            [DebuggerStepThrough]
            set
            {
                using (BinaryWriter bw = new BinaryWriter(new MemoryStream(FBuffer, true)))
                {
                    bw.BaseStream.Position = 4;
                    bw.Write(value.ToInt64());
                }
            }
        }

        /// <summary>
        /// Gets or sets the name of the volume.
        /// </summary>
        /// <exception cref="ArgumentNullException">The argument <paramref name="value"/> is <see langword="null"/>.</exception>
        public String VolumeName
        {
            [DebuggerStepThrough]
            get
            {
                int count;
                for (count = 0; (FBuffer[0x0C + count] != 0) && (count < 52); )
                    count++;
                try
                {
                    return System.Text.Encoding.UTF8.GetString(FBuffer, 0x0C, count);
                }
                catch (Exception)
                {
                    // Invalid sequence?
                    return String.Empty;
                }
            }
            [DebuggerStepThrough]
            set
            {
                if (value == null)
                    throw new ArgumentNullException("value");
                byte[] buffer = System.Text.Encoding.UTF8.GetBytes(value);
                // Limit at 52.
                Array.Copy(buffer, 0, FBuffer, 0x0C, Math.Min(51, buffer.Length));
                FBuffer[0x3F] = 0;
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Accepts a visitor.
        /// </summary>
        /// <param name="visitor">The visitor.</param>
        [DebuggerStepThrough]
        public override void AcceptVisitor(IIndexEntryVisitor visitor)
        {
            visitor.VisitVolumeIdentifier(this);
        }

        #endregion

        #region Constructors
        /// <overloads>Initializes a new instance of <see cref="VolumeIdentifierEntry"/>.</overloads>
        /// <summary>
        /// Initializes a new instance of <see cref="VolumeIdentifierEntry"/>.
        /// </summary>
        [DebuggerStepThrough]
        public VolumeIdentifierEntry(Int64 position, byte[] buffer)
            : base(position, buffer)
        {
        }
        #endregion
    }
}
